package com.exartisan.molisasurvey;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.ar.network.NetworkUtility;
import com.ar.network.RestConnector;
import com.ar.network.StupidParametersBuilder;
import com.exartisan.adapter.ContestPagerAdapter;
import com.exartisan.bean.Survey;
import com.exartisan.utils.ResponseParser;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class ContestActivity extends BaseActivity {
	private ArrayList<String> listContestName;
	private ArrayList<String> listStartDate;
	private ArrayList<String> listEndDate;
	private ViewPager contestPager;
	private PagerAdapter adapter;
	private ProgressDialog loadQuestionProgressDialog;
	private ArrayList<Survey> surveys;
	private String jsonResponse;
	private ProgressDialog loadContestProgressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void displayActivity() {
		setContentView(R.layout.activity_contest);
	}

	@Override
	protected void initViews() {
		contestPager = (ViewPager) findViewById(R.id.contestPager);

	}

	@Override
	protected void initVariables() {
		surveys = new ArrayList<Survey>();
	}

	@Override
	protected void initActions() {
		for (SherlockActivity a : ActivityHolder.allActivities) {
			if (!a.equals(this)) a.finish();
		}
		if(NetworkUtility.checkNetworkState(getApplicationContext())){
			loadContestProgressDialog = ProgressDialog.show(getCurrentActivity(), "Đang kết nối ...", "Đang tải danh sách cuộc thi, vui lòng chờ");
		RestConnector.post("", StupidParametersBuilder.BuildParams(StupidParametersBuilder.BuildLoadSurveyData(getUser().getUser_id())), new AsyncHttpResponseHandler(){
			
			public void onSuccess(int arg0, String response) {
				loadContestProgressDialog.dismiss();
				Log.d("zz", response);
				super.onSuccess(arg0, response);
				if (ResponseParser.checkSuccess(response)){
					jsonResponse = response;
					listContestName = new ArrayList<String>();
					listStartDate = new ArrayList<String>();
					listEndDate = new ArrayList<String>();
					//if (ResponseParser.checkSuccess(jsonResponse)) {
						surveys = ResponseParser.getContestData(jsonResponse);
						for (int i = 0; i < surveys.size(); i++) {
							listContestName.add(surveys.get(i).getSurveyName());
							listStartDate.add(surveys.get(i).getStartDate());
							listEndDate.add(surveys.get(i).getEndDate());
						}
					//}
					int[] logo = new int[surveys.size()];
					for (int i = 0; i < logo.length; i++) {
						if(i%2 == 0) logo[i] = R.drawable.daihoidoanx2;
						else
							logo[i] = R.drawable.huyhieu;
					}
					adapter = new ContestPagerAdapter(getCurrentActivity(), listContestName, listStartDate, listEndDate, logo, new OnClickListener() {
						
						public void onClick(View v) {
							getUser().setContestId(String.valueOf(surveys.get(v.getId()).getSurveyID()));
							Log.d("contest_id", StupidParametersBuilder.BuildLoadQuestionData(getUser().getUser_id(), String.valueOf(surveys.get(v.getId()).getSurveyID())));
							if (NetworkUtility.checkNetworkState(getApplicationContext())){
								loadQuestionProgressDialog = ProgressDialog.show(getCurrentActivity(), "Đang kết nối", "Đang tải câu hỏi, vui lòng chờ...");
								RestConnector.post("", StupidParametersBuilder.BuildParams(StupidParametersBuilder.BuildLoadQuestionData(getUser().getUser_id(), String.valueOf(surveys.get(v.getId()).getSurveyID()))), new AsyncHttpResponseHandler(){
									
									public void onSuccess(int arg0, String response) {
										loadQuestionProgressDialog.dismiss();
										super.onSuccess(arg0, response);
										Log.d("questionResponse", response);
										if(ResponseParser.checkSuccess(response)){
										Intent intent = new Intent(getCurrentActivity(), SurveyActivity.class);
										intent.putExtra("question data", response);
										startActivity(intent);
										}
										else
											NetworkUtility.showDialog(getCurrentActivity(), "Error", ResponseParser.getErrorMessage(response));
									};
									
									public void onFailure(Throwable arg0, String arg1) {
										loadContestProgressDialog.dismiss();
										NetworkUtility.showTimeOutDialog(getCurrentActivity());
										super.onFailure(arg0, arg1); 
									};
									
								});
							}
							else
								NetworkUtility.showNoNetworkDialog(getCurrentActivity());
						}
					});
					
					//adapter = new ContestPagerAdapter(this, listContestName, listStartDate, listEndDate, logo);
					contestPager.setAdapter(adapter);
				}
				else {
					//NetworkUtility.showDialog(getCurrentActivity(), "Error", ResponseParser.getErrorMessage(response));
					Toast.makeText(getApplicationContext(), ResponseParser.getErrorMessage(response), Toast.LENGTH_LONG).show();
					getUser().setUser_id(null);
					Intent i = new Intent(getApplicationContext(), LoginActivity.class);
					startActivity(i);
					finish();
				}
			}
			
			@Override
			public void onFailure(Throwable arg0, String arg1) {
				loadContestProgressDialog.dismiss();
				super.onFailure(arg0, arg1);
				Toast.makeText(getApplicationContext(), "Không thể kết nối đến server", Toast.LENGTH_LONG).show();
				//NetworkUtility.showTimeOutDialog(getCurrentActivity());
				finish();
			}
			
		});
		} else {
			Toast.makeText(getApplicationContext(), "Không có kết nối", Toast.LENGTH_LONG).show();
			finish();
		}
//		surveyManager = SurveyManager.getInstance();
//		listFileName = surveyManager.getListFileName();
		// Toast.makeText(this, listFileName.get(0), Toast.LENGTH_LONG).show();
		// listContestName = new ArrayList<String>();
		// for (String fileName : listFileName) {
		// listContestName.add(surveyManager.getSurveyName(fileName));
		// }
		// int[] logo = new int[]{R.drawable.daihoidoanx2,R.drawable.huyhieu};
		// adapter = new ContestPagerAdapter(this, listFileName,
		// listContestName, logo);
		// contestPager.setAdapter(adapter);
		
		
	}

}
